package com.jwinf.cdLibary.controller;


import com.jwinf.cdLibary.tool.WindowUtils;


import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;


import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;


/**
 * @author jwinf
 * 用户管理窗口控制类
 */
public class UserMainFrameController extends CdController implements Initializable {

    @FXML
    private Label borrowlbl;
    @FXML
    private Label returnlbl;
    @FXML
    private Label changeUser;



    // 初始化方法
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        componentInit();                // 组件初始化
        addListener();                  // 添加监听器
    }

    // 组件初始化
    private void componentInit(){

        // 给标签添加icon样式的图标
        Image image;
        image = new Image(getClass().getResourceAsStream("/icon/borrow.png"),64,64,false,false);
        borrowlbl.setGraphic(new ImageView(image));

        image = new Image(getClass().getResourceAsStream("/icon/return.png"), 64, 64, false, false);
        returnlbl.setGraphic(new ImageView(image));

        image = new Image(getClass().getResourceAsStream("/icon/exitandchange.png"), 32, 32, false, false);
        changeUser.setGraphic(new ImageView(image));


    }

    // 添加事件监听
    private void addListener(){
        // 鼠标进入组件时改变鼠标的样式
        WindowUtils.changeMouse(borrowlbl);
        WindowUtils.changeMouse(returnlbl);
        WindowUtils.changeMouse(changeUser);
        // 执行耗时操作时改变鼠标的样式
        WindowUtils.MouseWait(returnlbl);

        // 给标签添加事件监听 在鼠标进入时使图标变大 离开时变小
        borrowlbl.addEventHandler(MouseEvent.MOUSE_ENTERED, e ->{
            Image image;
            image = new Image(getClass().getResourceAsStream("/icon/borrow.png"), 80, 80, false, false);
            borrowlbl.setGraphic(new ImageView(image));
        });
        borrowlbl.addEventHandler(MouseEvent.MOUSE_EXITED, e ->{
            Image image;
            image = new Image(getClass().getResourceAsStream("/icon/borrow.png"), 64, 64, false, false);
            borrowlbl.setGraphic(new ImageView(image));
        });
        returnlbl.addEventHandler(MouseEvent.MOUSE_ENTERED, e->{
            Image image;
            image = new Image(getClass().getResourceAsStream("/icon/return.png"), 80, 80, false, false);
            returnlbl.setGraphic(new ImageView(image));
        });
        returnlbl.addEventHandler(MouseEvent.MOUSE_EXITED, e ->{
            Image image = new Image(getClass().getResourceAsStream("/icon/return.png"), 64, 64, false, false);
            returnlbl.setGraphic(new ImageView(image));
        });
        changeUser.addEventHandler(MouseEvent.MOUSE_ENTERED, e -> {
            Image image;
            image = new Image(getClass().getResourceAsStream("/icon/exitandchange.png"), 40, 40, false, false);
            changeUser.setGraphic(new ImageView(image));
        });
        changeUser.addEventHandler(MouseEvent.MOUSE_EXITED, e -> {
            Image image;
            image = new Image(getClass().getResourceAsStream("/icon/exitandchange.png"), 32, 32, false, false);
            changeUser.setGraphic(new ImageView(image));
        });
        changeUser.addEventHandler(MouseEvent.MOUSE_CLICKED, e-> {
            try {
                WindowUtils.showFrame("/com/jwinf/cdLibary/view/LoginFrame.fxml", "/icon/loginIcon.png", "Please Login");
                WindowUtils.destroyStage(changeUser);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });
    }

    // 借阅方法
    @FXML
    private void borrow() throws IOException {
        // 显示借阅窗体
        WindowUtils.showFrame("/com/jwinf/cdLibary/view/BorrowFrame.fxml", "/icon/loginIcon.png", "借阅", user);
        WindowUtils.destroyStage(borrowlbl);
    }

    // 归还方法
    @FXML
    private void returnCd() throws IOException {
        // 显示归还窗体
        WindowUtils.showReturnFrame(user);
        WindowUtils.destroyStage(returnlbl);
    }
}
